package com.controller.front_controller;


import com.mypg.Comment;
import com.mypg.User;
import com.service.CommentService;
import com.service.CommentValueService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/front_comment")
@Controller
public class FrontCommentController {

    @Autowired
    CommentService commentService;

    @Autowired
    UserService userService;

    @Autowired
    CommentValueService commentValueService;

    @RequestMapping("/insert_comment.do")
    @ResponseBody
    public Map<String,Object> doInsert_comment(HttpSession session,int question_id,String comment_des){
        Map<String,Object> map=new HashMap<>();
        String user_name= (String) session.getAttribute("user_name");
        int user_id=userService.selectUserByUser_name(user_name).getUser_id();
        Timestamp timestamp=new Timestamp(System.currentTimeMillis());
        Comment comment=new Comment();
        comment.setUser_id(user_id);
        comment.setQuestion_id(question_id);
        comment.setComment_des(comment_des);
        comment.setComment_time(timestamp);
        map.put("flag",1);
        try{
            commentService.insertComment(comment);
        }catch (Exception e){
            map.put("flag",0);
        }
        return map;
    }

    @RequestMapping("/change_comment.do")
    @ResponseBody
    public Map<String,Object> doChange_comment(HttpSession session,int question_id){
        String user_name= (String) session.getAttribute("user_name");
        int user_id=userService.selectUserByUser_name(user_name).getUser_id();
        Map<String,Object> map=new HashMap<>();
        List<Comment> commentList=commentService.selectCommentByQuestion_id(question_id);
        map.put("commentList",commentList);
        List<User> userList=new ArrayList<>();
        List<Integer> flagList=new ArrayList<>();
        for(int i=0;i<commentList.size();i++){
            User user=userService.selectUserByUser_id(commentList.get(i).getUser_id());
            flagList.add(commentValueService.selectCountByUser_idAndComment_id(user_id,commentList.get(i).getComment_id()));
            userList.add(user);
        }
        map.put("userList",userList);
        map.put("flagList",flagList);
        return map;
    }

    @RequestMapping("/update_comment_value.do")
    @ResponseBody
    public void doUpdate_comment_value(HttpSession session,int comment_id,int comment_value,int flag){
        commentService.updateComment_valueByComment_id(comment_id,comment_value);
        String user_name= (String) session.getAttribute("user_name");
        int user_id=userService.selectUserByUser_name(user_name).getUser_id();
        if(flag==1){
            //插入
            commentValueService.insertCommentValue(user_id,comment_id);
        }else{
            //删除
            commentValueService.deleteCommentValue(user_id,comment_id);
        }
    }
}
